package com.ruoyi.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.domain.TbDailyUnitPrice;
import com.ruoyi.basic.domain.TbExchangeRate;
import com.ruoyi.basic.domain.TbMonthlyExchangeRate;
import com.ruoyi.basic.mapper.TbMonthlyExchangeRateMapper;
import com.ruoyi.basic.service.ITbMonthlyExchangeRateService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.enums.BizExceptionEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 汇率Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-26
 */
@Service
public class TbMonthlyExchangeRateServiceImpl extends ServiceImpl<TbMonthlyExchangeRateMapper, TbMonthlyExchangeRate> implements ITbMonthlyExchangeRateService
{
    @Override
    public List<TbMonthlyExchangeRate> listByCondition(TbMonthlyExchangeRate tbMonthlyExchangeRate) {
        List<TbMonthlyExchangeRate> list = getBaseMapper().selectByCondition(tbMonthlyExchangeRate);
        return list;
    }

    @Override
    public int saveRate(TbMonthlyExchangeRate tbMonthlyExchangeRate) {
        check(tbMonthlyExchangeRate);
        return save(tbMonthlyExchangeRate) ? 1 : 0;
    }

    @Override
    public int updateRate(TbMonthlyExchangeRate tbMonthlyExchangeRate) {
        check(tbMonthlyExchangeRate);
        return updateById(tbMonthlyExchangeRate) ? 1 : 0;
    }

    @Override
    public boolean deleteByIds(String ids) {
        if(ObjectUtils.isEmpty(ids)){
            throw new ServiceException(BizExceptionEnum.REQUEST_PARAMETER_ERROR.getMessage());
        }
        LambdaQueryWrapper<TbMonthlyExchangeRate> wrapper = new LambdaQueryWrapper();
        wrapper.in(TbMonthlyExchangeRate::getId, Arrays.asList(ids.split(",")));
        return remove(wrapper);
    }

    @Override
    public List<TbMonthlyExchangeRate> listByMonth(String month) {
        LambdaQueryWrapper<TbMonthlyExchangeRate> wrapper = new LambdaQueryWrapper();
        wrapper.apply("date_format(rate_date,'%Y-%m') = {0}",month);
        return list(wrapper);
    }

    @Override
    public String checkRateMonth(TbMonthlyExchangeRate tbMonthlyExchangeRate) {
        List<TbMonthlyExchangeRate> list = listByMonth(tbMonthlyExchangeRate.getRateMonth());
        if (list.isEmpty())
        {
            return UserConstants.CATEGORY_NAME_NOT_UNIQUE;
        }
        return UserConstants.CATEGORY_NAME_UNIQUE;
    }

    @Override
    public TbMonthlyExchangeRate getByFromTo(String from,String to,String rateMonth) {
        LambdaQueryWrapper<TbMonthlyExchangeRate> wrapper = new LambdaQueryWrapper();
        wrapper.apply("date_format(rate_date,'%Y-%m') >= {0}",rateMonth)
        .eq(TbMonthlyExchangeRate::getFrom,from)
        .eq(TbMonthlyExchangeRate::getTo,to);
        TbMonthlyExchangeRate monthlyExchangeRate = list(wrapper).isEmpty() ? null : list(wrapper).get(0);
        return monthlyExchangeRate;
    }

    private void check(TbMonthlyExchangeRate tbMonthlyExchangeRate) {
        LambdaQueryWrapper<TbMonthlyExchangeRate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbMonthlyExchangeRate::getFrom, tbMonthlyExchangeRate.getFrom())
                .eq(TbMonthlyExchangeRate::getTo, tbMonthlyExchangeRate.getTo())
                .apply(" date_format(rate_date,'%Y-%m') = {0} ", DateUtils.parseDateToStr("yyyy-MM",tbMonthlyExchangeRate.getRateDate()))
                .orderByDesc(TbMonthlyExchangeRate::getCreateTime);
        List<TbMonthlyExchangeRate> list = list(wrapper);
        TbMonthlyExchangeRate old = list.isEmpty() ? null : list.get(0);

        if (old != null && !old.getId().equals(tbMonthlyExchangeRate.getId())) {
            throw new ServiceException("月汇率已存在");
        }
    }
}
